﻿using FB.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FB.Providers.EmployeeInfo
{
    public class EmployeeInfoUnit : UnitOfWorkBase<EMPDbContext>
    {
        Hashtable _repositories;

        GenericRepository<Employee> _employee = null;
        GenericRepository<ProfilePic> _profilePic = null;
        GenericRepository<Company> _company = null;
        GenericRepository<Department> _department = null;
        GenericRepository<EmployeeType> _employeeType = null;
        GenericRepository<Position> _position = null;
        GenericRepository<Sequence> _sequence = null;
        GenericRepository<Team> _team = null;
        GenericRepository<EmployeeInPosition> _employeeInPosition = null;
        
        

        internal EmployeeInfoUnit(EMPDbContext context) : base(context) { }


        protected virtual GenericRepository<T> GetRepository<T>(ref GenericRepository<T> _rpo) where T : class
        {
            if (_rpo == null) _rpo = new GenericRepository<T>(base._context);
            return _rpo;
        }


        #region repository

        public IRepository<Employee> EmployeeRepository { get { return GetRepository(ref _employee); } }
        public IRepository<ProfilePic> ProfilePicRepository { get { return GetRepository(ref _profilePic); } }
        public IRepository<Company> CompanyRepository { get { return GetRepository(ref _company); } }
        public IRepository<Department> DepartmentRepository { get { return GetRepository(ref _department); } }
        public IRepository<EmployeeType> EmployeeTypeRepository { get { return GetRepository(ref _employeeType); } }
        public IRepository<Position> PositionRepository { get { return GetRepository(ref _position); } }
        public IRepository<Sequence> SequenceRepository { get { return GetRepository(ref _sequence); } }
        public IRepository<Team> TeamRepository { get { return GetRepository(ref _team); } }
        public IRepository<EmployeeInPosition> EmployeeInPositionRepository { get { return GetRepository(ref _employeeInPosition); } }


        public GenericRepository<T> Repository<T>() where T : class
        {
            if (_repositories == null)
                _repositories = new Hashtable();

            var type = typeof(T).Name;

            if (!_repositories.ContainsKey(type))
            {
                var repositoryType = typeof(GenericRepository<>);

                var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), _context);
                _repositories.Add(type, repositoryInstance);
            }

            return (GenericRepository<T>)_repositories[type];
        }

        #endregion


        public bool SetLocation(string EMPCode, int? LocationID)
        {
            var result = false;

            var detail = Repository<Employee>().GetByKey(EMPCode);

            if (detail != null)
            {
                detail.LocationID = LocationID;

                Repository<Employee>().Update(detail);

                result = SaveChanges() > 0;
            }

            return result;
        }

    }

    
    public class GenericRepository<TEntity> : Repository<EMPDbContext, TEntity> where TEntity : class
    {
        public GenericRepository(EMPDbContext context) : base(context) { }
    }
}
